# -*-coding: utf-8 -*-
from concurrent import futures
import grpc
import logging
import time

from DeepFmApi_pb2 import DeepResp
from DeepFmApi_pb2_grpc import DeepFmService, \
  add_DeepFmServiceServicer_to_server


class DeepFmServiceImpl(DeepFmService):

  def test(self, request, context):
    print("请求参数：")
    print(request)
    return DeepResp(
        message='Hello, %s!' % request.model_name,
        code=request.model_version
    )


def serve():
  # 这里通过thread pool来并发处理server的任务
  server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))

  # 将对应的任务处理函数添加到rpc server中
  add_DeepFmServiceServicer_to_server(DeepFmServiceImpl(), server)

  # 这里使用的非安全接口，实际上gRPC支持TLS/SSL安全连接，以及各种鉴权机制
  server.add_insecure_port('[::]:50000')
  print("启动服务 localhost:50000")
  server.start()
  print("监听端口:50000")
  try:
    while True:
      time.sleep(60 * 60 * 24)
  except KeyboardInterrupt:
    server.stop(0)


if __name__ == "__main__":
  logging.basicConfig()
  serve()
